from django.db import models
from django.forms import ModelForm, TextInput, NumberInput, Select


# Create your models here.

class Dept(models.Model):
    name = models.CharField(max_length=100, unique=True)
    address = models.CharField(max_length=100)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'dept'

    def __str__(self):
        return self.name


class User(models.Model):
    username = models.CharField(max_length=100, unique=True, verbose_name='用户名')
    password = models.CharField(max_length=100)
    age = models.IntegerField(verbose_name='年龄')
    # 性别  1 男  2 女    国际化处理  ModelForm   下拉选    数据转换
    gender = models.IntegerField(choices=((1, '男'), (2, '女'), (3, '其它')), verbose_name='性别')
    dept = models.ForeignKey(Dept, on_delete=models.PROTECT, verbose_name='部门')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'user'


# 创建User的表单类
class UserForm(ModelForm):
    class Meta:
        model = User
        # 声明需要生成表单的属性 __all__
        fields = ['username', 'age', 'gender', 'dept']

        # 插件设置
        widgets = {
            'username': TextInput(attrs={'class': 'form-control'}),
            'age': NumberInput(attrs={'class': 'form-control'}),
            'gender': Select(attrs={'class': 'form-control'}),
            'dept': Select(attrs={'class': 'form-control'}),
        }


class Employee(models.Model):
    emp_no = models.CharField(max_length=100, unique=True, verbose_name='员工号')
    emp_name = models.CharField(max_length=100, verbose_name='姓名')
    age = models.IntegerField(verbose_name='年龄')
    salary = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='薪资')
    dept = models.ForeignKey(Dept, on_delete=models.PROTECT)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'employee'


# emp 表单对象
class EmployeeForm(ModelForm):
    class Meta:
        model = Employee
        fields = ['emp_no', 'emp_name', 'age', 'salary', 'dept']
        widgets = {
            'emp_no': TextInput(attrs={'class': 'form-control'}),
            'emp_name': TextInput(attrs={'class': 'form-control'}),
            'age': NumberInput(attrs={'class': 'form-control'}),
            'salary': NumberInput(attrs={'class': 'form-control'}),
            'dept': Select(attrs={'class': 'form-control'}),
        }
